package webUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Signer {
	
	private static final Log logger = LogFactory.getLog(Signer.class);
	
	
	/**
	 * 获取MD5加签
	 * @param body 商户请求body体
	 * @return
	 */
	public static String getSign4MD5(String body){

		StringBuffer buf = new StringBuffer("");
		try {  
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(body.getBytes());
			byte b[] = md.digest();
			int i;
			for (int index = 0; index < b.length; index++) {
				i = b[index];
				if (i < 0){
					i += 256;
				}
				if (i < 16){
					buf.append("0");
				}
				buf.append(Integer.toHexString(i));
			}
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();  
			return null;  
		}
		return buf.toString();
	}
	
	/**
	 * 对map进行key的升序排列
	 * @param map
	 * @return
	 */
	public static String sortMap(Map<String, String> map){
			List<Map.Entry<String, String>> infoIds = new ArrayList<Map.Entry<String, String>>(map.entrySet());
			Collections.sort(infoIds, new Comparator<Map.Entry<String, String>>() {   
			    public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {      
			        return (o1.getKey()).toString().compareTo(o2.getKey());
			    }
			}); 
			
			StringBuffer params = new StringBuffer();
			
			for (int i = 0; i < infoIds.size(); i++) {
			    String id = infoIds.get(i).toString();
			    params.append(id).append("&");
			}
			
			return params.toString();
	}
	 /** 
     * 除去数组中的空值
     * @param sArray 签名参数组
     * @return 去掉空值的新签名参数组
     */
    public static Map<String, String> paraFilter(Map<String, String> sArray) {

        Set set = sArray.keySet();  
        for (Iterator iterator = set.iterator(); iterator.hasNext();) {  
            Object obj = (Object) iterator.next();  
            Object value =(Object)sArray.get(obj);  
            remove(value, iterator);  
        }  
       
        return sArray;
    }
    
    private static void remove(Object obj,Iterator iterator){  
        if(obj instanceof String){  
            String str = (String)obj;  
            if(StringUtils.isEmpty(str)){  
                iterator.remove();  
            }  
        }else if(obj instanceof Collection){  
            Collection col = (Collection)obj;  
            if(col==null||col.isEmpty()){  
                iterator.remove();  
            }  
              
        }else if(obj instanceof Map){  
            Map temp = (Map)obj;  
            if(temp==null||temp.isEmpty()){  
                iterator.remove();  
            }  
              
        }else if(obj instanceof Object[]){  
            Object[] array =(Object[])obj;  
            if(array==null||array.length<=0){  
                iterator.remove();  
            }  
        }else{  
            if(obj==null){  
                iterator.remove();  
            }  
        }  
    }  
}